package org.glorylight.hacks.social.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.glorylight.hacks.social.data.InvitationData;
import org.glorylight.hacks.social.data.MessageData;
import org.glorylight.hacks.social.data.NotificationData;
import org.glorylight.hacks.social.data.PictureData;
import org.glorylight.hacks.social.data.ScrapBookCommentsData;
import org.glorylight.hacks.social.data.ScrapBookData;
import org.glorylight.hacks.social.data.ScrapBookVotesData;
import org.glorylight.hacks.social.data.TipsData;
import org.glorylight.hacks.social.data.UserData;
import org.glorylight.hacks.social.data.UserNetworkData;
import org.glorylight.hacks.social.model.InvitationLog;
import org.glorylight.hacks.social.model.Message;
import org.glorylight.hacks.social.model.Notification;
import org.glorylight.hacks.social.model.Picture;
import org.glorylight.hacks.social.model.ScrapBook;
import org.glorylight.hacks.social.model.ScrapBookComments;
import org.glorylight.hacks.social.model.ScrapBookVotes;
import org.glorylight.hacks.social.model.Tips;
import org.glorylight.hacks.social.model.TipsDetail;
import org.glorylight.hacks.social.model.User;
import org.glorylight.hacks.social.model.UserNetwork;
import org.joda.time.LocalDate;
import org.joda.time.Period;
import org.joda.time.PeriodType;

public class ModelHelper {
	
	public static ScrapBook convertScrapBookData(ScrapBookData data) {
		return new ScrapBook(UUIDHelper.stringToUUID(data.getId()), data.getUserId(), data.getDateCreated(), data.getPicPath(), 
					data.getTitle(), data.getDesc(), data.isShared());
	}
	
	public static ScrapBookData convertScrapBook(ScrapBook data) {
		return new ScrapBookData(UUIDHelper.uuidToString(data.getId()), data.getUserId(), data.getDateCreated(), data.getPicPath(), 
					data.getTitle(), data.getDescription(), data.isShared());
	}
	
	public static User convertUserData(UserData data) {
		return new User(data.getEmailAddress(), ArrayUtils.toObject(data.getPassword().getBytes()), data.getFirstName(), data.getLastName(), data.getBirthDate(), data.getGender(), data.getMobileNo(), data.getRegistrationDate(), data.isActivated());
	}
	
	public static UserData convertUser(User data) {		
		return new UserData(data.getEmailAddress(), new String(ArrayUtils.toPrimitive(data.getPassword())), data.getFirstName(), data.getLastName(), data.getBirthDate(), data.getGender(), data.getMobileNo(), data.getRegistrationDate(), data.isActivated());
	}
	
	public static Picture convertPictureData(PictureData data) {
		return new Picture(data.getId(),data.getUrl(), data.getUserId(), data.getTitle(), 
					data.getDescription(), data.getCreatedDate());
	}
	
	public static PictureData convertPicture(Picture data) {
		return new PictureData(data.getId(), data.getPicPath(), data.getUserId(), data.getTitle(), 
					data.getDescription(), data.getDateCreated());
	}
	
	public static Notification convertNotificationData(NotificationData data) {
		return new Notification(data.getId(),UUIDHelper.stringToUUID(data.getNotifyUser()), data.getScrapBookId(), 
					data.getFriendId(),data.getDateCreated(), data.getType());
	}
	
	public static NotificationData convertNotification(Notification data) {
		return new NotificationData(data.getId(),UUIDHelper.uuidToString(data.getNotifyUser()), data.getScrapBookId(),
					data.getNetworkId(),data.getDateCreated(), data.getType());
	}
	
	public static UserNetworkData convertUserNetwork(UserNetwork data) {
		return new UserNetworkData(data.getId(), data.getUser(), UUIDHelper.uuidToString(data.getConnectTo()), data.getDateCreated());
	}
	
	public static UserNetwork convertUserNetworkData(UserNetworkData data) {
		return new UserNetwork(data.getId(), data.getUser(), UUIDHelper.stringToUUID(data.getConnectTo()), data.getDateConnected());
	}
	
	public static MessageData convertMessage(Message data) {
		return new MessageData(data.getId(), data.getSender(), data.getRecipient(), data.getSubject(), 
					data.getDateCreated(), data.getDescription(), data.isNew());
	}
	
	public static Message convertMessageData(MessageData data) {
		return new Message(data.getId(), data.getSender(), data.getRecipient(), data.getSubject(), 
					data.getDateCreated(), data.getDescription(), data.isNew());
	}
	
	public static TipsData convertTips(Tips data, TipsDetail detailData) {
		return new TipsData(data.getId(), data.getStartAge(), data.getEndAge(), data.getTags(), data.getTitle(), detailData.getContentDetail());
	}
	
//	public static Tips convertTipsData(TipsData data) {
//		return new Tips(data.getId(), data.getStartAge(), data.getEndAge(), data.getTags(), data.getTitle());
//	}
//	
//	public static TipsDetailData convertTipsDetail(TipsDetail data) {
//		return new TipsDetailData(data.getId(), data.getContent());
//	}
//	
//	public static TipsDetail convertTipsDetailData(TipsDetailData data) {
//		return new TipsDetail(data.getId(), data.getContent());
//	}
	
	public static InvitationLog convertInvitationData(InvitationData data) {
		return new InvitationLog(data.getId(), data.getSender(), data.getRecipient(), data.getDateCreated());
	}
	
	public static InvitationData convertInvitationLog(InvitationLog data) {
		return new InvitationData(data.getId(), data.getSender(), data.getRecipient(), data.getDateCreated());
	}
	
	public float getBabyAge(Date babyBirthDate) {
		Calendar babyDate = Calendar.getInstance();
		babyDate.setTime(babyBirthDate);
		
		
		Calendar nowDate = Calendar.getInstance();
		nowDate.setTime(new Date());
		
		LocalDate baby = new LocalDate(babyDate.getTimeInMillis());
		LocalDate now = new LocalDate(nowDate.getTimeInMillis());
		
		Period period = new Period(baby, now, PeriodType.yearMonthDay());
		
		float age = period.getYears();
		
		if (period.getMonths() > 4 ) age = (float) (age + 0.5);
		
		return age;
	}
	
	public static ScrapBookVotesData convertScrapBookVoters(ScrapBookVotes data) {
		return new ScrapBookVotesData(UUIDHelper.uuidToString(data.getUuid()), data.getVoter().getFirstName(), data.getVoter().getLastName(), UUIDHelper.uuidToString(data.getVoter().getUserId()));
	}

	public static List<ScrapBookVotesData> convertScrapBookVoters(List<ScrapBookVotes> voters) {
		List<ScrapBookVotesData> votersData = new ArrayList<ScrapBookVotesData>();
		for(ScrapBookVotes scrapBookVotes : voters) {
			votersData.add(convertScrapBookVoters(scrapBookVotes));
		}
		return votersData;
	}
	
	public static ScrapBookCommentsData convertScrapBookComments(ScrapBookComments data) {
		return new ScrapBookCommentsData(UUIDHelper.uuidToString(data.getUuid()), data.getVoter().getFirstName(), data.getVoter().getLastName(), UUIDHelper.uuidToString(data.getVoter().getUserId()), data.getComment(), data.getDateCreated());
	}

	public static List<ScrapBookCommentsData> convertScrapBookComments(List<ScrapBookComments> comments) {
		List<ScrapBookCommentsData> commentsData = new ArrayList<ScrapBookCommentsData>();
		for(ScrapBookComments comment : comments) {
			commentsData.add(convertScrapBookComments(comment));
		}
		return commentsData;
	}
}
